package scau.online.web.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.fasterxml.jackson.databind.deser.Deserializers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import scau.cty.base.common.BaseController;
import scau.cty.base.common.IBaseService;
import scau.cty.dictionary.Code;
import scau.cty.entity.Brand;
import scau.cty.service.IBrandService;
import java.math.BigDecimal;
import java.util.*;
/**
 * Created by caoxiong on 2017/4/19.
 */
@Api(value = "品牌信息管理控制器",description = "品牌信息接口")
@RestController
@RequestMapping(value = "/admin_brand", produces = "application/json")
public class AdminBrandController extends BaseController{
    @Autowired
    private IBaseService<Brand> brandIBaseService;

    /**
     * 获取品牌列表 根据pageSize和pageNow查询 order为排序方式
     * @param pageSize  每页数量
     * @param pageNow   当前页
     * @param order     排列方式
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "品牌列表接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageNow",value = "请求页码",required = true,paramType = "query"),
            @ApiImplicitParam(name = "order",value = "排序方式，不传参数或参数为0为默认id排序，其他情况按照名字name排序",
                    required = true,paramType = "query")})
    @GetMapping(value = "/brand_list")
    public String getBrandList(Integer pageSize,Integer pageNow,Integer order) throws Exception{
        Map map = new LinkedHashMap<>();
        if(pageNow == null || pageNow == 0){
            pageNow = 1;
        }
        if(pageSize == null || pageSize ==0){
            sendCode(map, Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        if(order == null || order == 0){
            //默认排序
            List<Brand> brandList = brandIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"id");
            map.put("brandList",brandList);
        }else {
            List<Brand> brandList = brandIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"name");
            map.put("brandList",brandList);
        }
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }

    /**
     * 通过 品牌 id 查询该品牌的信息
     * @param id
     * @return
     */
    @ApiOperation(value = "品牌详情接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "品牌唯一标识id",required = true,paramType = "query")})
    @GetMapping(value = "/brand_column")
    public String selectBrand(String id){
        Map map = new LinkedHashMap();
        if(id == null || id ==""){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        Brand brandColumn = brandIBaseService.findByKey(id);
        map.put("brandColumn", brandColumn);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }
    /**
     *  根据指定商品 id 删除品牌
     * @param id
     * @return
     */
    @ApiOperation(value = "删除品牌接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "品牌唯一标识id",required = true,paramType = "query")})
    @PostMapping(value = "/brand_delete")
    public String deleteBrandById(String id){
        Map map = new LinkedHashMap();
        if(id == null || id == ""){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        int deleteBrand = brandIBaseService.delete(id);
        map.put("deleteBrand",deleteBrand);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }

    /**
     * 修改品牌信息 通过品牌唯一 id 标识修改
     * id
     * name
     * logo
     * @return
     */
    @ApiOperation(value = "更新品牌信息接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "品牌唯一标识id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "name",value = "品牌名",required = true,paramType = "query"),
            @ApiImplicitParam(name = "logo",value = "品牌logo",required = true,paramType = "query")})
    @PostMapping(value = "/brand_update")
    public String updateBrandById(Brand brand){
        Map map = new LinkedHashMap();
        if(brand.getId() == null || brand.getId() == ""){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        if(brand.getName() == null && brand.getLogo() == null){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        int updateBrand = brandIBaseService.updateNotNull(brand);
        map.put("updateBrand",updateBrand);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }

    /**
     * 增加品牌 对应的 id 自动生成
     *  name
     *  logo
     * @return
     */
    @ApiOperation(value = "增加品牌信息接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "name",value = "品牌名",required = true,paramType = "query"),
            @ApiImplicitParam(name = "logo",value = "品牌logo",required = true,paramType = "query")})
    @PostMapping(value = "/brand_add")
    public String addBrand(Brand brand){
        Map map = new LinkedHashMap();
        if(brand.getName() == null || brand.getName() == "" || brand.getLogo() == null || brand.getLogo() == ""){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        int addBrand = brandIBaseService.add(brand);
        map.put("addBrand",addBrand);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }
}
